<?php

class CodeWriter
{
	//contador interno de indentación
	private $indent;
	private $indent_enabled;
	//donde se va almacenando el contenido final de salida (la clase compilada)
	private $buffer;
	
	
	public function toString()
	{
		return $this->buffer;
	}
	
	public function tabs()
	{
		$this->buffer.=str_repeat("\t", $this->indent);
	}
	
	public function write($str)
	{
		$this->buffer.=$str;
	}
	
	public function line($str="")
	{
		if(func_num_args()==0)
		{
			$this->buffer.="\n";
		}
		else
		{
			$this->buffer.=str_repeat("\t", $this->indent).$str."\n";
			if($this->indent_enabled)
			{
				if(ends_with($str, "{")) $this->indent++;
				else if(ends_with($str, "}")) $this->indent--;
			}
		}
	}
	
	//insertar un bloque multilínea
	public function insert($str)
	{
		$lines = explode("\n", $str);
		$this->indent_enabled = false;
		foreach($lines as $line)
		{
			$this->line($line);
		}
		$this->indent_enabled = true;
	}
	
	function __construct()
	{
		$this->indent = 0;
		$this->buffer = "";
		$this->indent_enabled = true;
	}

	function dumpArray($arr)
	{
	}
	
}

?>